package cn.gov.ssxmzj.service;

import cn.gov.ssxmzj.entry.Result;
import cn.gov.ssxmzj.entry.VillageInfo;
import cn.gov.ssxmzj.mapper.VillageMapper;
import cn.gov.ssxmzj.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class VillageService {

    @Autowired
    private VillageMapper mapper;

    /**
     * 获取乡镇信息
     * @param villageId
     * @return
     */
    public Result getVillageInfo(String villageId,boolean isManger){
        villageId = Utils.isEmpty(villageId)?null:villageId;
        Map sqlMap = Utils.asMap("villageId",villageId,"validTag","1");
        List<VillageInfo> villageInfos = mapper.getVillageInfo(sqlMap);
        if (Utils.isEmpty(villageInfos)){
            return new Result(Result.SERVICE_ERROR,"未查询到乡镇信息",
                    "<div style='text-align:center;'>未查询到乡镇信息</div>");
        }
        String villageInfo = "";
        if(isManger){
            villageInfo += "<div class=\"layui-input-inline\">" +
                    "<select id=\"villages\" lay-filter=\"village-filter\" name=\"villages\" lay-verify=\"required\" lay-search=\"villages\">" +
                    "<option value=\"0\">请选择或输入乡镇名</option>";
            for(VillageInfo village:villageInfos){
                villageInfo += "<option value=\""+village.getVillage_id()+"\">"+village.getVillage_name()+"</option>";
            }
            villageInfo += "</select></div>";
            return new Result(Result.SUCCESS,"查询成功",villageInfo+"");
        }
        //全部乡镇按钮默认被选中,且单独一行
        villageInfo = "<button id='checkedButton' style='margin-right: 10px;' itemid=''><a href=\"javascript:;\">" +
                "<i class=\"fa fa-file-text-o\" aria-hidden=\"true\">" +
                "</i>&nbsp;&nbsp;全部乡镇</a></button>";
        for(VillageInfo village:villageInfos){
            villageInfo += "<button class='checked' itemid='"+village.getVillage_id()+"'><a href=\"javascript:;\">" +
                    "<i class=\"fa fa-file-text-o\" aria-hidden=\"true\">" +
                    "</i>&nbsp;&nbsp;"+village.getVillage_name()+"</a></button>";
        }
        return new Result(Result.SUCCESS,"查询成功",villageInfo+"");
    }
}
